﻿Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.BusinessLayer.Services
Imports SISSAT.BusinessLayer.Utilities

Namespace Views
    Public Class EmployeeDetailControlPresenter

        Private Property View As IEmployeeDetailControlView
        Private Service As IEmployeeService

        Public Sub New(view As IEmployeeDetailControlView)
            Service = New EmployeeService
            Me.View = view
            AddHandler Me.View.OnClear, AddressOf OnClear
            AddHandler Me.View.OnInitialization, AddressOf OnInitialization
            AddHandler Me.View.OnSubmit, AddressOf OnSubmit
        End Sub

        Private Sub OnClear()
            Me.View.ContactNumber = String.Empty
            Me.View.Course = Convert.ToString(1)
            Me.View.EmailAddress = String.Empty
            Me.View.EmployeeCode = String.Empty
            Me.View.FirstName = String.Empty
            Me.View.HomeAddress = String.Empty
            Me.View.LastName = String.Empty
            Me.View.MiddleName = String.Empty
            Me.View.Year = String.Empty

            OnInitialization()
        End Sub

        Private Sub OnInitialization()
            Dim employeeCode As String = Me.View.EmployeeCode
            Dim employeeId As Integer = Me.View.EmployeeId

            If String.IsNullOrEmpty(employeeCode) AndAlso employeeId < 1 Then
                Return
            End If

            Dim entity As Employee = Nothing
            If employeeId > 0 Then
                entity = Service.ReadById(employeeId)
            Else
                entity = Service.ReadByCode(employeeCode)
            End If

            Me.View.ContactNumber = entity.ContactNumber
            Me.View.Course = Convert.ToString(entity.Course.CourseId)
            Me.View.EmailAddress = entity.EmailAddress
            Me.View.EmployeeCode = entity.EmployeeCode
            Me.View.FirstName = entity.FirstName
            Me.View.HomeAddress = entity.HomeAddress
            Me.View.LastName = entity.LastName
            Me.View.MiddleName = entity.MiddleName
            Me.View.Year = Convert.ToString(entity.Year)
        End Sub

        Private Sub OnSubmit()
            Dim courseId As Integer = Convert.ToInt32(Me.View.Course)
            Dim year As Integer = Convert.ToInt32(Me.View.Year)
            Dim employeeCode As String = Me.View.EmployeeCode

            If String.IsNullOrEmpty(employeeCode) Then
                If Me.View.EmployeeTypeId = 0 Then
                    Throw New ArgumentNullException("EmployeeTypeId", "EmployeeTypeId is missing")
                End If

                Dim semester As Semesters = CType([Enum].Parse(GetType(Semesters), Me.View.CurrentSemester.ToString()), Semesters)
                Dim newEmployeeCode As String = _
                    EmployeeCodeHelper.GenerateEmployeeCode(courseId, year, Me.View.CurrentSchoolYear, semester)
                Dim employeeId As Integer = _
                    Service.Create(newEmployeeCode, Me.View.FirstName, Me.View.MiddleName, Me.View.LastName, Me.View.HomeAddress, Me.View.ContactNumber, Me.View.EmailAddress, courseId, Me.View.EmployeeTypeId, year, Me.View.CurrentUserId)
                Me.View.EmployeeCode = newEmployeeCode
                Me.View.EmployeeId = employeeId

                Return
            End If

            Dim entity As Employee = Service.ReadByCode(employeeCode)
            entity.ContactNumber = Me.View.ContactNumber
            entity.HomeAddress = Me.View.HomeAddress
            entity.EmailAddress = Me.View.EmailAddress
            Service.Edit(entity, Me.View.CurrentUserId)
        End Sub

    End Class
End Namespace

